package cp.remote.controller;

import cp.remote.R;
import cp.remote.activity.Remote_Activity;
import cp.remote.config.NetworkConfig;
import cp.remote.network.RemoteControll;
import cp.remote.network.RemoteNetWorker;
import cp.remote.network.TaskSwitch;
import cp.remote.timer.CampusBotNetworkTimer;
import android.graphics.Bitmap;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.*;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class ManuallCarControll implements OnSeekBarChangeListener,
        OnTouchListener, OnClickListener {

    private RemoteNetWorker rem;
    private Remote_Activity act;
    private boolean waitforkeyrelease = false, twinpresed = false;
    private int lastkeyid = -1, twinid = -1, oldprogress = 0;
    Bitmap img;

    public ManuallCarControll(RemoteNetWorker rem,
            Remote_Activity remote_Activity) {
        this.rem = rem;
        this.act = remote_Activity;
    }

    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        act.setSpeedText(progress);
    }

    public void onStartTrackingTouch(SeekBar arg0) {
    }

    public void onStopTrackingTouch(SeekBar arg0) {
        if (!waitforkeyrelease){
            oldprogress = arg0.getProgress();
            sendCommand(RemoteControll.SPEED_.name()
                    + String.format("%03d", oldprogress));
        }else
            arg0.setProgress(oldprogress);
    }

    public boolean onTouch(View arg0, MotionEvent arg1) {
        switch (arg0.getId()) {
        case R.id.remote_button_up_l:
        case R.id.remote_button_up_r:
            sendCommand(arg1, RemoteControll.FRONT_DOWN, arg0.getId());
            break;
        case R.id.remote_button_down_l:
        case R.id.remote_button_down_r:
            sendCommand(arg1, RemoteControll.REAR_DOWN, arg0.getId());
            break;
        case R.id.remote_button_left_l:
            sendCommand(arg1, RemoteControll.LEFT_DOWN, arg0.getId());
            break;
        case R.id.remote_button_right_r:
            sendCommand(arg1, RemoteControll.RIGHT_DOWN, arg0.getId());
            break;
        default:
            NetworkConfig.nettask.send(TaskSwitch.STOP);
            act.setStateText("Sending STOP");
            CampusBotNetworkTimer.startHandler(act, NetworkConfig.nettask);
        }

        return false;
    }

    private void sendCommand(MotionEvent event, RemoteControll rightDown, int id) {
        if (rem != null) {
            if (event.getAction() == MotionEvent.ACTION_DOWN
                    && !waitforkeyrelease) {
                lastkeyid = id;
                waitforkeyrelease = true;
                rem.send(rightDown);
                changeButtonImg(id, true);
                act.setStateText("Sending Signal => " + rightDown);
                CampusBotNetworkTimer.startHandler(act, rem);
            } else if (event.getAction() == MotionEvent.ACTION_UP
                    && id == lastkeyid && !twinpresed) {
                rem.send(RemoteControll.KEY_RELEASED);
                act.setStateText("Sending Signal => "
                        + RemoteControll.KEY_RELEASED);
                CampusBotNetworkTimer.startHandler(act, rem);
                waitforkeyrelease = false;
                changeButtonImg(id, false);
            } else if (isSiteChange(id)) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    twinpresed = true;
                    twinid = id;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    twinpresed = false;
                    lastkeyid = (lastkeyid == id) ? twinid : id;
                }
            }
        }
    }

    private void sendCommand(String rightDown) {
        if (rem != null) {
            rem.send(rightDown);
            act.setStateText("Sending Signal => " + rightDown);
            CampusBotNetworkTimer.startHandler(act, rem);
        }
    }

    public void onClick(View v) {
        NetworkConfig.nettask.send(TaskSwitch.STOP);
    }

    private boolean isSiteChange(int id) {
        // Check for Button twin
        // ex. Left and Right up are the same
        if (((id == R.id.remote_button_down_l || id == R.id.remote_button_down_r) && (lastkeyid == R.id.remote_button_down_l || lastkeyid == R.id.remote_button_down_r))
                || ((id == R.id.remote_button_up_l || id == R.id.remote_button_up_r) && (lastkeyid == R.id.remote_button_up_l || lastkeyid == R.id.remote_button_up_r))) {
            return true;
        }
        return false;
    }

    private void changeButtonImg(int pressedbuttonid, boolean down) {
        switch (pressedbuttonid) {
        case R.id.remote_button_up_l:
        case R.id.remote_button_up_r:
            if (down) {
                act.setButtonImage(R.id.remote_button_up_l, R.drawable.red_up);
                act.setButtonImage(R.id.remote_button_up_r, R.drawable.red_up);
            } else {
                act.setButtonImage(R.id.remote_button_up_l, R.drawable.green_up);
                act.setButtonImage(R.id.remote_button_up_r, R.drawable.green_up);
            }
            break;
        case R.id.remote_button_down_l:
        case R.id.remote_button_down_r:
            if (down) {
                act.setButtonImage(R.id.remote_button_down_l,
                        R.drawable.red_down);
                act.setButtonImage(R.id.remote_button_down_r,
                        R.drawable.red_down);
            } else {
                act.setButtonImage(R.id.remote_button_down_l,
                        R.drawable.green_down);
                act.setButtonImage(R.id.remote_button_down_r,
                        R.drawable.green_down);
            }
            break;
        case R.id.remote_button_left_l:
            if (down) {
                act.setButtonImage(R.id.remote_button_left_l,
                        R.drawable.red_left);
            } else {
                act.setButtonImage(R.id.remote_button_left_l,
                        R.drawable.green_left);
            }
            break;
        case R.id.remote_button_right_r:
            if (down) {
                act.setButtonImage(R.id.remote_button_right_r,
                        R.drawable.red_right);
            } else {
                act.setButtonImage(R.id.remote_button_right_r,
                        R.drawable.green_right);
            }
            break;
        }
    }
}
